import {Toast} from 'antd-mobile'
import {createGoUserReq, loginGoUserReq, logoutReq,} from '../apis/goApis'
import {isLogin, setLoginLocalStorage, clearLoginLocalStorage} from "../utils/storageService"
import {handleResp} from '../utils/commonfunc'
import {encryptPwd} from "../utils/encryptService"
import LocalStorageService from "../utils/LocalStorageService";
import React from 'react'

export const global = {
    state: {
        user: {},
        isLogin: isLogin || false,
        isLoading: false,
        current: window.location.href,
    },

    reducers: {

        doLoginSuccess(state, payload) {
            return {
                ...state,
                user: payload,
                isLogin: true,
            }
        },

        showInfo(state, payload) {
            Toast.info(payload.info, payload.duration)
            return state
        },

        showLoading(state) {
            Toast.loading("loading", 0)
            return {...state, isLoading: true}
        },

        globalError(state, payload) {
            Toast.info(payload, 2)
            return state
        },

        stopLoading(state) {
            return {...state, isLoading: false}
        },

        onCreateUsersuccess(state, payload) {
            return {
                ...state,
                user: payload,
                isLogin: true,
            }
        },

        onAuthFailed(state, payload) {
            let storage = LocalStorageService
            storage.clear()

            return {...state, user: {}, isLogin: false}
        },

        onLogout(state, payload) {
            // clearLoginLocalStorage()
            let storage = LocalStorageService
            storage.clear()
            return {...state, user: {}, isLogin: false}
        },
    },

    effects: (dispatch) => ({

        async doLoginGo(payload, rootState) {
            payload.pwd = encryptPwd(payload.pwd)
            dispatch.global.showLoading()
            const resp = await loginGoUserReq(payload)
            handleResp(resp, dispatch, (data) => {
                setLoginLocalStorage(data)
                dispatch.global.doLoginSuccess(data)
                // dispatch
                dispatch.msg.doFetchMsg({type: 'sys', uid: LocalStorageService.get('userid'),})
                dispatch.global.stopLoading()
            })
        },

        async doRegGo(payload, rootState) {
            // dispatch.global.showLoading()
            const resp = await createGoUserReq(payload)
            const p = new Promise(resolve => {

                handleResp(resp, dispatch, (data) => {
                    if (data.userid) {
                        setLoginLocalStorage(data)
                        const info = <div>
                            <p>新客登录</p>
                            <p>经验+1</p>
                        </div>
                        Toast.info(info, 1)
                        setTimeout(() => {
                            dispatch.global.onCreateUsersuccess(data)
                            dispatch.msg.doFetchMsg({type: 'sys', uid: LocalStorageService.get('userid'),})
                            resolve(true)
                            // dispatch.msg.doFetchMsg({type: 'vip', uid: LocalStorageService.get('userid'),})
                        }, 1000)
                    } else {
                        resolve(false)
                    }
                })
            })
            return p
        },

        async doLogoutGo(payload, rootState) {
            dispatch.global.showLoading()
            const resp = await logoutReq(payload)
            handleResp(resp, dispatch, (data) => {
                if (data.ok === 1) {
                    dispatch.global.onLogout()
                    dispatch.user.onLogout()
                    dispatch.global.stopLoading()
                }
            })
        },
    })
}
